package com.hongawen.runnable;

import com.hongawen.thread.ThreadDownNetPics;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2021年08月13日 19:07
 */
public class RunnableDiwnNetPic implements Runnable{
    public String url;

    public String name;

    public RunnableDiwnNetPic(String url,String name){
        this.url=url;
        this.name=name;
    }

    public RunnableDiwnNetPic(){ }

    /**
     * 下载网络图片
     */
    @Override
    public void run() {
        this.webDownPic(url,name);
        System.out.println("下载了图片，图片名为："+name);
    }

    public static void main(String[] args) {

        RunnableDiwnNetPic runnableDiwnNetPic1 = new RunnableDiwnNetPic("https://tse2-mm.cn.bing.net/th/id/OIP-C.xsA-3qUw6cqmd8nRfxk6TQHaEK?w=308&h=180&c=7&o=5&pid=1.7", "1.png");
        RunnableDiwnNetPic runnableDiwnNetPic2 = new RunnableDiwnNetPic("https://tse2-mm.cn.bing.net/th/id/OIP-C.xsA-3qUw6cqmd8nRfxk6TQHaEK?w=308&h=180&c=7&o=5&pid=1.7", "2.png");
        RunnableDiwnNetPic runnableDiwnNetPic3 = new RunnableDiwnNetPic("https://tse2-mm.cn.bing.net/th/id/OIP-C.xsA-3qUw6cqmd8nRfxk6TQHaEK?w=308&h=180&c=7&o=5&pid=1.7", "3.png");

        new Thread(()->{
            runnableDiwnNetPic1.webDownPic(runnableDiwnNetPic1.url,runnableDiwnNetPic1.name);
        }).start();

        new Thread(()->{
            runnableDiwnNetPic2.webDownPic(runnableDiwnNetPic2.url,runnableDiwnNetPic2.name);
        }).start();

        new Thread(()->{
            runnableDiwnNetPic3.webDownPic(runnableDiwnNetPic3.url,runnableDiwnNetPic3.name);
        }).start();
    }

    public void webDownPic(String url,String name)  {
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
            System.out.println("下载了图片，图片名为："+name);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

}
